package com.ruoyi.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.bo.HtWorkloadBo;
import com.ruoyi.demo.domain.vo.HtWorkHoursVo;
import com.ruoyi.demo.domain.vo.HtWorkloadImportVo;
import com.ruoyi.demo.domain.vo.HtWorkloadVo;
import com.ruoyi.demo.listener.HtWorkloadImportListener;
import com.ruoyi.demo.service.IHtWorkloadService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 热处理工艺报
 *
 * @author ruoyi
 * @date 2024-03-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/workload")
public class HtWorkloadController extends BaseController {

    private final IHtWorkloadService iHtWorkloadService;

    /**
     * 查询热处理工艺报列表
     */
    @SaCheckPermission("demo:workload:list")
    @GetMapping("/list")
    public TableDataInfo<HtWorkloadVo> list(HtWorkloadBo bo, PageQuery pageQuery) {
        return iHtWorkloadService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出全部热处理工艺报列表
     */
    @SaCheckPermission("demo:workload:export")
    @Log(title = "热处理工艺报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(HtWorkloadBo bo, HttpServletResponse response) {
        List<HtWorkloadVo> list = iHtWorkloadService.queryList(bo);
        ExcelUtil.exportExcel(list, "热处理工艺报", HtWorkloadVo.class, response);
    }

    /**
     * 导出热处理工艺报表
     */
    @SaCheckPermission("demo:workload:export")
    @Log(title = "热处理工艺报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStatistics")
    public void exportStatistics(HtWorkloadBo bo, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        Map<String, Object> multiListMap = iHtWorkloadService.exportStatistics(bo);
        List<Object> dataList = (List<Object>) multiListMap.get("data");
        Map<String, Object> mapData = (Map<String, Object>) multiListMap.get("map");
        ExcelUtil.exportTemplateMap(CollUtil.newArrayList(map, dataList), mapData, "复杂单列表.xlsx", "excel/statistics.xlsx", response);
    }

    /**
     * 导出热处理工时报表
     */
    @SaCheckPermission("demo:workload:export")
    @Log(title = "热处理工艺报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportWorkHours")
    public void exportWorkHours(HtWorkloadBo bo, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        List<HtWorkHoursVo> list = iHtWorkloadService.exportWorkHours(bo);
//        ExcelUtil.exportTemplate(CollUtil.newArrayList( map, list ),"复杂单列表.xlsx", "excel/workHours.xlsx", response);
        ExcelUtil.exportExcel(list, "热处理工艺报", HtWorkHoursVo.class, response);
    }

    /**
     * 导出选择热处理工艺报列表
     */
    @SaCheckPermission("demo:workload:export")
    @Log(title = "热处理工艺报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{ids}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> ids, HttpServletResponse response) {
        List<HtWorkloadVo> list = iHtWorkloadService.queryByIds(ids);
        ExcelUtil.exportExcel(list, "热处理工艺报", HtWorkloadVo.class, response);
    }

    /**
     * 获取热处理工艺报详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("demo:workload:query")
    @GetMapping("/{id}")
    public R<HtWorkloadVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iHtWorkloadService.queryById(id));
    }

    /**
     * 新增热处理工艺报
     */
    @SaCheckPermission("demo:workload:add")
    @Log(title = "热处理工艺报", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HtWorkloadBo bo) {
        return toAjax(iHtWorkloadService.insertByBo(bo));
    }

    /**
     * 修改热处理工艺报
     */
    @SaCheckPermission("demo:workload:edit")
    @Log(title = "热处理工艺报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HtWorkloadBo bo) {
        return toAjax(iHtWorkloadService.updateByBo(bo));
    }

    /**
     * 删除热处理工艺报
     *
     * @param ids 主键串
     */
    @SaCheckPermission("demo:workload:remove")
    @Log(title = "热处理工艺报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iHtWorkloadService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "项目得分",  HtWorkloadImportVo.class, response);
    }

    /**
     * 导入热处理工艺报数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "热处理工艺报", businessType = BusinessType.IMPORT)
    @SaCheckPermission("demo:workload:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<HtWorkloadImportVo> result = ExcelUtil.importExcel(file.getInputStream(), HtWorkloadImportVo.class, new HtWorkloadImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
}
